
import java.awt.Color;
import java.awt.Cursor;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import edu.jhu.tcj.purfuit.Game;
import edu.jhu.tcj.purfuit.utils.GameFile;

public class SelectionPanel extends GamePanel {

	private Image img;
	private int selected = 0;
	private final SoundManager soundManager;
	private final Application application;

	public SelectionPanel(Application application, SoundManager soundManager) {
		this.application = application;
		this.soundManager = soundManager;

		ImageIcon imageIcon = new ImageIcon(getClass().getResource("images/bg.jpg"));
		img = imageIcon.getImage();
	}

	@Override
	public void keyPressed(KeyEvent ke) {
		if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
			doAction();
		}
		if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
			soundManager.changeSelectedSound();

			selected++;
			if (selected > 2)
				selected = 0;
			repaint();

		}

		if (ke.getKeyCode() == KeyEvent.VK_UP) {
			soundManager.changeSelectedSound();

			selected--;
			if (selected < 0)
				selected = 2;
			repaint();

		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		int x = e.getPoint().x;
		int y = e.getPoint().y - 50;
		setCursor(Cursor.getDefaultCursor());
		if (x > 340 && x < 490) {
			if (y > 270 && y < 300) {
				doAction();
			}
			if (y > 320 && y < 350) {
				doAction();
			}
			if (y > 370 && y < 400) {
				doAction();
			}
		}

	}

	@Override
	public void mouseMoved(MouseEvent e) {

		// System.out.println(e.getPoint().x + " " + e.getPoint().y);
		int x = e.getPoint().x;
		int y = e.getPoint().y - 50;
		setCursor(Cursor.getDefaultCursor());
		if (x > 340 && x < 490) {
			if (y > 270 && y < 300) {
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				if (selected != 0) {
					soundManager.changeSelectedSound();
					selected = 0;
					repaint();
				}
			}
			if (y > 320 && y < 350) {
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				if (selected != 1) {
					soundManager.changeSelectedSound();
					selected = 1;
					repaint();
				}
			}
			if (y > 370 && y < 400)

			{
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				if (selected != 2) {
					soundManager.changeSelectedSound();
					selected = 2;
					repaint();
				}
			}
		}
	}

	public void doAction() {
		soundManager.playSelectNoise();
		if (selected == 0) {
			System.out.println("starting new game");
			application.showPlayerSetupPanel();
		} else if (selected == 1) {
			System.out.println("loading game");
			FileDialog dialog = new FileDialog(application);
			dialog.setVisible(true);
			String directory = dialog.getDirectory();
			String file = dialog.getFile();
			System.out.println("Selected File: " + directory + file);
			try {
				GameFile gf = new GameFile();
				Game loadedGame = gf.loadGame(directory + file);
				application.showGameBoardPanel(loadedGame);
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "Unable to load game");
			} 
		} else if (selected == 2) {
			application.exitApplication();
		}
	}

	public void paintComponent(Graphics g) {

		g.setColor(new Color(255, 212, 0));
		g.drawOval(0, 0, getWidth(), getHeight());
		if (img != null)
		g.drawImage(img, getWidth() / 2 - img.getWidth(null) / 2, getHeight() / 2
				- img.getHeight(null) / 2, null);

		if (selected == 0) {
			g.setColor(Color.red);
		}
		g.setFont(new Font("Tahoma", Font.PLAIN, 30));
		g.drawString("New Game", 340, 300);
		g.setColor(new Color(255, 212, 0));
		if (selected == 1) {
			g.setColor(Color.red);
		}
		g.drawString("Load Game", 335, 350);
		g.setColor(new Color(255, 212, 0));
		if (selected == 2) {
			g.setColor(Color.red);
		}
		g.drawString("Exit Game", 340, 400);
		g.setColor(new Color(255, 212, 0));
	}
}
